﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TiebaSpider.Beans
{
	public class PagePartition : INotifyPropertyChanged
	{
		private String _range;
		public String Range
		{
			get
			{
				return _range;
			}
			set
			{
				_range = value;
				OnPropertyChanged(new PropertyChangedEventArgs("Range"));
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
		public void OnPropertyChanged(PropertyChangedEventArgs e)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, e);
		}
	}

	public class ProxyPartition
	{
		public ObservableCollection<PagePartition> Partitions { get; set; }
		private IDictionary<String, Tuple<int, int>> Ranges { get; set; }

		public ProxyPartition()
		{
			Partitions = new ObservableCollection<PagePartition>();
			Ranges = new Dictionary<String, Tuple<int, int>>();
		}

		public ProxyPartition(Collection<Tuple<int, int>> partitionList)
			:this()
		{
			refreshPraitions(partitionList);
		}

		private void refreshPraitions(Collection<Tuple<int, int>> list)
		{
			Partitions.Clear();
			Ranges.Clear();
			foreach (Tuple<int, int> item in list)
			{
				String key = String.Format("{0:d} ~ {1:d}", item.Item1, item.Item2);
				PagePartition newPartition = new PagePartition();
				newPartition.Range = key;
				Partitions.Add(newPartition);
				Ranges.Add(key, item);
			}
		}

		public void LoadPartition(Collection<Tuple<int, int>> list)
		{
			refreshPraitions(list);
		}

		public void ClearAll()
		{
			Partitions.Clear();
			Ranges.Clear();
		}

		public Tuple<int, int> FindRange(String value)
		{
			return Ranges[value];
		}

		public bool HasRange(String value)
		{
			return Ranges.ContainsKey(value);
		}
	}
}
